package com.bsit.obu.utils;

import android.text.TextUtils;
import android.util.Log;


import com.bsit.obu.base.BaseApplication;
import com.bsit.obu.model.car.ObuCarInfo;

import java.io.UnsupportedEncodingException;

/**
 * Created by dreamer on 2016/12/16.
 */

public class CarInfoUtil {
    private static String TAG = "CarInfoParseUtil";


    /**
     * 打包车辆信息
     * @param data
     * @return
     */
    public static byte[] packageCarInfoData(ObuCarInfo data) {
        int index = 0;
        Log.e(TAG,"info is : " + data);
        if (data == null){
            return new byte[1];
        }
        byte[] carBytes = new byte[79];
        /*车牌号　12*/
        byte[] carNoByteResult = new byte[12];
        try {
            byte[] carNo = data.getmPlateNumber().getBytes("GB2312");
            System.arraycopy(carNo, 0 ,carNoByteResult, 0 ,carNo.length);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.arraycopy(carNoByteResult, 0, carBytes, 0, carNoByteResult.length);
        index += carNoByteResult.length;

        /*车牌颜色　2*/
        carBytes[index++] = (byte)0x00;
        carBytes[index++] = parseColor2Byte(data.getmPlateColor());
        /*车型　1*/
        carBytes[index++] = parseVechicleType2Byte( data.getmVehicleType());           //车辆类型
        /*车辆用户类型 1*/
        carBytes[index++] = Byte.parseByte(CarInfoUtil.getOperType(data.getmVehiccleUserType()));
        /*长宽高 4*/
        String carSize = data.getmLengthWidthHeight();
        String lenthStr = carSize.split("\\*")[0];
        String widthStr = carSize.split("\\*")[1];
        String heightStr = carSize.split("\\*")[2];
        int lenth = Integer.parseInt(lenthStr);
        byte width = Byte.parseByte(widthStr);
        byte height =Byte.parseByte(heightStr);
        carBytes[index++] = (byte) (lenth >> 8 & 0xFF);
        carBytes[index++] = (byte) (lenth & 0xFF);
        carBytes[index++] = (byte) (width & 0xFF);
        carBytes[index++] = (byte) (height & 0xFF);          //长宽高

        /*车轮数 1*/
        carBytes[index++] = Byte.parseByte(data.getmWheelNumber());   //车轮数
        /*车轴数 1*/
        carBytes[index++] = Byte.parseByte(data.getmMaxleNumber());          //车轴数
        /*轴距 2*/
        int distance = Integer.parseInt(data.getmVehicleWheelbase());
        carBytes[index++] = (byte) (distance >> 8 & 0xFF);
        carBytes[index++] = (byte) (distance & 0xFF);            //车轴距

        /*载重或座位数 3*/

        long weightOrSeat = Long.parseLong(data.getmLoadWeight());
        carBytes[index++] = (byte) (weightOrSeat >> 16 & 0xFF);
        carBytes[index++] = (byte) (weightOrSeat >> 8 & 0xFF);
        carBytes[index++] = (byte) (weightOrSeat & 0xFF);                  //载重座位数

        /*车辆描述 16*/
        if (TextUtils.isEmpty(data.getmVehicleDescription())){
            ToastUtils.showToast(BaseApplication.getInstance(), "请输入车辆描述信息！");
            return new byte[1];
        }
        String carDescriptionStr = data.getmVehicleDescription();

//        String carDescriptionStr = stringToAscii(carDescription);
        byte[] cmdDescription = new byte[0];
        try {
            cmdDescription = carDescriptionStr.getBytes("GB2312");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        MyLog.e(TAG, ByteUtil.byte2HexStr(cmdDescription) + " ASCII is : " + carDescriptionStr);
        if (cmdDescription.length > 16){
            ToastUtils.showToast(BaseApplication.getInstance(), "描述过长不得超过16字，请重新输入");
            return new byte[1];
        } else {
            int i = 0;
            for(; i < cmdDescription.length; i++){
                carBytes[index++] = cmdDescription[i];
            }
            for (;i < 16; i++){
                carBytes[index++] = 0;
            }
        }

        /*发动机编号 16*/
        if (TextUtils.isEmpty(data.getmVehicleDescription())){
            ToastUtils.showToast(BaseApplication.getInstance(), "请输入发动机编号信息！");
            return new byte[1];
        }
        String carEngineNumberStr = data.getmEngineNumber();
//        String carEngineNumberStr = stringToAscii(carEngineNumber);
        byte[] cmdEngineNumber = new byte[0];
        try {
            cmdEngineNumber = carEngineNumberStr.getBytes("GB2312");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (cmdEngineNumber.length > 16){
            ToastUtils.showToast(BaseApplication.getInstance(), "发动机编号输入过长，请重新输入16字以内");
            return new byte[1];
        } else {
            int i = 0;
            for(; i < cmdEngineNumber.length; i++){
                carBytes[index++] = cmdEngineNumber[i];
            }
            for (;i < 16; i++){
                carBytes[index++] = 0;
            }
        }

        for (int i = 0;i < 20; i++){
            carBytes[index++] = (byte)0x00;
        }
        Log.e(TAG,index + " 车辆码为： " + ByteUtil.byte2HexStr(carBytes));
        return carBytes;
    }


    /**
     * 根据颜色获取车牌颜色对应字节
     * @param carColor
     * @return
     */
    public static String parseColor2ByteStr(String carColor) {
        switch (carColor){
            case "蓝色":
                return "00";
            case "黄色":
                return "01";
            case "黑色":
                return "02";
            case "白色":
                return "03";
            case "灰色":
                return "04";
            case "青色":
                return "05";
            case "绿色":
                return "06";
            case "棕色":
                return "08";
            case "紫色":
                return "09";
            case "红色":
                return "07";
        }
        return "FF";
    }

    /**
     * 获取用户对应的字节
     * @param type
     * @return
     */
    public static byte parseCustomerByte(String type) {
        switch (type){
            case "个人":
                return (byte)0x00;
            case "单位":
                return (byte)0x01;
        }
        return (byte)0xFF;
    }
    /**
     * 根据颜色获取车牌颜色对应字节
     * @param carColor
     * @return
     */
    public static byte parseColor2Byte(String carColor) {
        switch (carColor){
            case "蓝色":
                return (byte)0x00;
            case "黄色":
                return (byte)0x01;
            case "黑色":
                return (byte)0x02;
            case "白色":
                return (byte)0x03;
            case "渐变绿色":
                return (byte)0x04;
            case "黄绿双拼色":
                return (byte)0x05;
            case "蓝白渐变色":
                return (byte)0x06;
        }
        return (byte)0xFF;
    }
    /**
     * 根据车型获取车牌颜色对应字节
     * 01：客一；02：客二；03：客三；04：客四；11：货一；12：货二；13：货三；14：货四；15：货五；16：货六；21：集一
     * @param carType
     * @return
     */
    public static byte parseVechicleType2Byte(String carType) {
        switch (carType){
            case "一客":
                return  (byte)0x01;
            case "二客":
                return  (byte)0x02;
            case "三客":
                return  (byte)0x03;
            case "四客":
                return  (byte)0x04;
            case "一货":
                return  (byte)0x11;
            case "二货":
                return (byte)0x12;
            case "三货":
                return (byte)0x13;
            case "四货":
                return (byte)0x14;
            case "五货":
                return (byte)0x15;
            case "一型专项":
                return (byte)0x21;
            case "二型专项":
                return (byte)0x22;
            case "三型专项":
                return (byte)0x23;
            case "四型专项":
                return (byte)0x24;
        }
        return (byte)0x00;
    }

    /**
     * 根据车型获取车牌颜色对应字节字符串
     * 01：客一；02：客二；03：客三；04：客四；11：货一；12：货二；13：货三；14：货四；15：货五；16：货六；21：集一
     * @param carType
     * @return
     */
    public static String parseVechicleType2Str(String carType) {
        switch (carType){
            case "客一":
                return "01";
            case "客二":
                return "02";
            case "客三":
                return "03";
            case "客四":
                return "04";
            case "货一":
                return "11";
            case "货二":
                return "12";
            case "货三":
                return "13";
            case "货四":
                return "14";
            case "货五":
                return "15";
            case "货六":
                return "16";
            case "集一":
                return "21";
        }
        return "08";
    }

    /**
     * 根据车型获取车牌颜色对应字节
     * @param carType
     * @return
     */
    public static byte parseUserType2Byte(String carType) {
        switch (carType){
            case "普通车" :
                return 0;
            /*case "公务车" :
                return 6;
            case "军警车" :
                return 8;
            case "紧急车" :
                return 10;
            case "免费" :
                return 12;
            case "车队" :
                return 14;
            case "其他" :
                return 3;*/
            default:
                return 7;
        }
    }

    public static String getCarSize(String carSize){
        byte [] carBytes = new byte[6];
        int index = 0;
        String lenthStr = carSize.split("\\*")[0];
        String widthStr = carSize.split("\\*")[1];
        String heightStr = carSize.split("\\*")[2];
        /*int lenth = Integer.parseInt(lenthStr);
        int width = Byte.parseByte(widthStr);
        int height = Byte.parseByte(heightStr);
        String lenStr = String.valueOf(lenth);
        String widStr = String.valueOf(width);
        String heiStr = String.valueOf(height);*/
        String str = "0000";
        String result = str.substring(0,4 - lenthStr.length())+ lenthStr + str.substring(0, 4 - widthStr.length())+ widthStr + str.substring(0, 4 - heightStr.length())+ heightStr;
        /*carBytes[index++] = (byte) (lenth >> 8 & 0xFF);
        carBytes[index++] = (byte) (lenth & 0xFF);
        carBytes[index++] = 0;
        carBytes[index++] = (byte) (width & 0xFF);
        carBytes[index++] = 0;
        carBytes[index++] = (byte) (height & 0xFF);*/
        return /*ByteUtil.byte2HexStr(carBytes)*/result;
    }

    public static String getOperType(String operType){
        switch (operType){
            case "运营车辆":
                return "1";
            case "非运营车辆":
                return "2";
            default:
                return "0";
        }
    }
    public static String getCertType(String certType){
        switch (certType){
            case "身份证":
                return "0";
            case "军官证":
                return "1";
            case "护照":
                return "2";
            case "入境证":
                return "3";
            case "临时身份证":
                return "4";
            case "营业执照":
                return "5";
            case "组织机构代码证":
                return "6";
            case "机关法人证书":
                return "7";
            case "上级单位核准通知书":
                return "8";
            default:
                return "9";
        }
    }
    public static String getUserType(String userType){
        switch (userType){
            case "不记名":
                return "0";
            case "个人用户":
                return "1";
            case "集团用户":
                return "2";
            default:
                return "9";
        }
    }

}
